package com.central.user.service.impl;

import com.central.annotation.datasource.DataSource;
import com.central.model.common.PageResult;
import com.central.model.common.utils.PageUtil;
import com.central.user.dao.BsdldDao;
import com.central.user.dao.PlayerBaseDao;
import com.central.user.model.PlayerBaseInfo;
import com.central.user.model.PlayerDetailsInfo;
import com.central.user.service.PlayerBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PlayerBaseServiceImpl implements PlayerBaseService {

    @Autowired
    private PlayerBaseDao playerBaseDao;

    @Autowired
    private BsdldDao bsdldDao;

    @Override
    public PageResult<PlayerBaseInfo> findPlayerBaseInfoByBsdld(Map<String, Object> params,boolean isPaging) {
        System.out.println("战斗");
        for (Object obj: params.values()){
            System.out.println(obj);
        }
        int total = bsdldDao.countPlayer(params);
        List<PlayerBaseInfo> list = Collections.emptyList();
        if (total > 0) {
            isPaging(params,isPaging);
            list = bsdldDao.findPlayerInfo(params);
        }
        return PageResult.<PlayerBaseInfo>builder().data(list).code(0).count((long)total).build();
    }

    @Override
    @DataSource(name = "three")
    public PageResult<PlayerBaseInfo> findPlayerBaseInfoByLogin(Map<String, Object> params,boolean isPaging) {
        System.out.println("登录");
        for (Object obj: params.values()){
            System.out.println(obj);
        }
        int total = playerBaseDao.findCount(params);
        List<PlayerBaseInfo> list = Collections.emptyList();
        if (total > 0) {
            isPaging(params,isPaging);
            list = playerBaseDao.findPlayerInfo(params);
        }
        return PageResult.<PlayerBaseInfo>builder().data(list).code(0).count((long)total).build();
    }

    /**
     * 是否需要分页,若不需要,则移除分页信息
     * @param params
     * @param isPaging
     */
    private void isPaging(Map<String, Object> params,boolean isPaging){
        if (!isPaging){
            params.remove("page");
            params.remove("limit");
        }else {
            PageUtil.pageParamConver(params, true);
        }
    }


    @DataSource(name = "three")
    @Override
    public PlayerDetailsInfo findByPlayerId(String playerId) {
        List<PlayerDetailsInfo> baseInfos = playerBaseDao.findByPlayerId(playerId);
        if (baseInfos.size() == 1){
            return baseInfos.get(0);
        }
        return null;
    }
}
